﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CommandBase.h"
#include "GbmpUiPlatformCommandIds.h"
#include "IApplication.h"
#include "IUiDocumentViewManager.h"
#include "CreateGBMPFamilyInstanceAction.h"
#include "IUiDocument.h"
#include "IActionManager.h"
#include "GbmpUiPlatformCommandRegister.h"
#include "ISelection.h"

#include "IUserTransaction.h"
#include "IInstance.h"
#include "CommandParameters.h"
#include "UiDocumentViewUtils.h"
#include "GcmpCommandState.h"

#include "IFamily.h"
#include "IElementBasicInformation.h"
#include "GcmpBuiltInCategoryUniIdentities.h"
#include "GbmpActionCreatePointFamilyInstance.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

namespace {
    gcmp::CommandParameterValue GetCmdParam(const gcmp::CommandParameters& cmdParams, const std::wstring& key)
    {
        auto itor = cmdParams.find(key);
        DBG_WARN_AND_RETURN_UNLESS(itor != cmdParams.end(), gcmp::CommandParameterValue(), L"没有找到所需参数",L"GDMPLab",L"2024-03-30");
        return itor->second;
    }
}

//////////////////////////////////////////////////////////////////////////
class CmdCreatePointFamilyInstanceOnWrokplane : public CommandBase
{
public:
    CmdCreatePointFamilyInstanceOnWrokplane();
    virtual ~CmdCreatePointFamilyInstanceOnWrokplane();
public:
    virtual gcmp::OwnerPtr<IAction> ExecuteCommand(const gcmp::CommandParameters& cmdParams) override;

    virtual bool IsVisible() const override;
    virtual bool IsEnabled() const override;
};

CmdCreatePointFamilyInstanceOnWrokplane::CmdCreatePointFamilyInstanceOnWrokplane()
    : CommandBase(ID_CMD_CREATE_POINTFAMILY_INSTANCE_ON_WORKPLANE)
{
}


CmdCreatePointFamilyInstanceOnWrokplane::~CmdCreatePointFamilyInstanceOnWrokplane()
{
}

gcmp::OwnerPtr<IAction> CmdCreatePointFamilyInstanceOnWrokplane::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    std::wstring wsCommandString = this->GetLastCommandData(CMDDATATYPE_COMMAND_STRING);
    std::wstring familyName;
    std::wstring familyTypeName;
    if (cmdParams.size()>0)
    {
        familyName =GetCmdParam(cmdParams, L"FamilyName").AsWString();
        familyTypeName = GetCmdParam(cmdParams, L"FamilyType").AsWString();
    }
    else if (wsCommandString == ID_CMD_CREATE_POINTFAMILY_INSTANCE_ON_FACE)
    {
        familyName = this->GetLastCommandData(CMDDATATYPE_FAMILY_NAME);
        familyTypeName = this->GetLastCommandData(CMDDATATYPE_FAMILY_TYPE_NAME);
    }
    else
    {
        return nullptr;
    }

    IUiDocument* pUIDoc = IUiDocumentViewManager::Get()->GetCurrentUiDocument();
    DBG_WARN_AND_RETURN_UNLESS(pUIDoc != nullptr, nullptr, L"pUIDoc为空",L"GDMPLab",L"2024-03-30");
    //gcmp::IDocument* pDoc = pUIDoc->GetDbDocument();
    IDocument* pDoc = pUIDoc->GetDbDocument();
    DBG_WARN_AND_RETURN_UNLESS(pDoc != nullptr, nullptr, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    const IFamily* pFamily = IFamily::GetFamilyByName(pDoc, familyName);
    if(pFamily)
    {
        const IFamilyType* pFamilyType = pFamily->GetFamilyTypeByName(familyTypeName);
        DBG_WARN_AND_RETURN_UNLESS(pFamilyType, nullptr, L"CmdSetOnePointInstanceOnWrokplane命令参数值不正确",L"GDMPLab",L"2024-03-30");
    }
    DBG_WARN_AND_RETURN_UNLESS(pFamily, nullptr, L"CmdSetOnePointInstanceOnWrokplane命令参数值不正确",L"GDMPLab",L"2024-03-30");

    std::map<int, std::wstring> commandData;
    commandData.insert(std::make_pair((int)CMDDATATYPE_FAMILY_NAME, familyName));
    commandData.insert(std::make_pair((int)CMDDATATYPE_FAMILY_TYPE_NAME, familyTypeName));
    commandData.insert(std::make_pair((int)CMDDATATYPE_BUILTIN_CATEGORY_ID, BuiltInCategoryUniIdentities::BICU_POINT_FAMILY.GetGuidString()));
    commandData.insert(std::make_pair((int)CMDDATATYPE_COMMAND_STRING, GetCommandId()));
    this->SaveCurrentCommandData(commandData);
    
    return NEW_AS_OWNER_PTR(ActionCreatePointFamilyInstance, EWS_On_Workplane, pFamily->GetBasicInformation()->GetElementId(), familyTypeName);
}

bool CmdCreatePointFamilyInstanceOnWrokplane::IsVisible() const
{
    return gcmp::GcmpCommandState::IsInProjectEnvironment() && !gcmp::GcmpCommandState::IsInAnimationEnvironment();
}

bool CmdCreatePointFamilyInstanceOnWrokplane::IsEnabled() const
{
    return gcmp::GcmpCommandState::IsInProjectEnvironment() && !gcmp::GcmpCommandState::IsInAnimationEnvironment();
}

REGISTER_GM_COMMAND(CmdCreatePointFamilyInstanceOnWrokplane);

//////////////////////////////////////////////////////////////////////////
class CmdCreatePointFamilyInstanceOnFace : public CommandBase
{
public:
    CmdCreatePointFamilyInstanceOnFace();
    ~CmdCreatePointFamilyInstanceOnFace();
public:
    virtual gcmp::OwnerPtr<IAction> ExecuteCommand(const gcmp::CommandParameters& cmdParams) override;

    virtual bool IsVisible() const override;
    virtual bool IsEnabled() const override;
};

CmdCreatePointFamilyInstanceOnFace::CmdCreatePointFamilyInstanceOnFace()
    : CommandBase(ID_CMD_CREATE_POINTFAMILY_INSTANCE_ON_FACE)
{
}

CmdCreatePointFamilyInstanceOnFace::~CmdCreatePointFamilyInstanceOnFace()
{
}

gcmp::OwnerPtr<IAction> CmdCreatePointFamilyInstanceOnFace::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    std::wstring wsCommandString = this->GetLastCommandData(CMDDATATYPE_COMMAND_STRING);
    std::wstring familyName;
    std::wstring familyTypeName;
    if(cmdParams.size()>0)
    {
        familyName = GetCmdParam(cmdParams, L"FamilyName").AsWString();
        familyTypeName = GetCmdParam(cmdParams, L"FamilyType").AsWString();
    }
    else if(wsCommandString == ID_CMD_CREATE_POINTFAMILY_INSTANCE_ON_WORKPLANE)
    {
        familyName = this->GetLastCommandData(CMDDATATYPE_FAMILY_NAME);
        familyTypeName = this->GetLastCommandData(CMDDATATYPE_FAMILY_TYPE_NAME);
    }
    else
    {
        return nullptr;
    }

    IUiDocument* pUIDoc = IUiDocumentViewManager::Get()->GetCurrentUiDocument();
    DBG_WARN_AND_RETURN_UNLESS(pUIDoc != nullptr, nullptr, L"pUIDoc为空",L"GDMPLab",L"2024-03-30");

    IDocument* pDoc = pUIDoc->GetDbDocument();
    DBG_WARN_AND_RETURN_UNLESS(pDoc != nullptr, nullptr, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    const IFamily* pFamily = IFamily::GetFamilyByName(pDoc, familyName);
    if(pFamily)
    {
        const IFamilyType* pFamilyType = pFamily->GetFamilyTypeByName(familyTypeName);
        DBG_WARN_AND_RETURN_UNLESS(pFamilyType, nullptr, L"CmdSetOnePointInstanceOnFace命令参数值不正确",L"GDMPLab",L"2024-03-30");
    }
    DBG_WARN_AND_RETURN_UNLESS(pFamily, nullptr, L"CmdSetOnePointInstanceOnFace命令参数值不正确",L"GDMPLab",L"2024-03-30");

    std::map<int, std::wstring> commandData;
    commandData.insert(std::make_pair((int)CMDDATATYPE_FAMILY_NAME, familyName));
    commandData.insert(std::make_pair((int)CMDDATATYPE_FAMILY_TYPE_NAME, familyTypeName));
    commandData.insert(std::make_pair((int)CMDDATATYPE_BUILTIN_CATEGORY_ID, BuiltInCategoryUniIdentities::BICU_POINT_FAMILY.GetGuidString()));
    commandData.insert(std::make_pair((int)CMDDATATYPE_COMMAND_STRING, GetCommandId()));
    this->SaveCurrentCommandData(commandData);

    return NEW_AS_OWNER_PTR(ActionCreatePointFamilyInstance, EWS_On_Face, pFamily->GetBasicInformation()->GetElementId(), familyTypeName);
}

bool CmdCreatePointFamilyInstanceOnFace::IsVisible() const
{
    return gcmp::GcmpCommandState::IsInProjectEnvironment() && !gcmp::GcmpCommandState::IsInAnimationEnvironment();
}

bool CmdCreatePointFamilyInstanceOnFace::IsEnabled() const
{
    return gcmp::GcmpCommandState::IsInProjectEnvironment() && !gcmp::GcmpCommandState::IsInAnimationEnvironment();
}

REGISTER_GM_COMMAND(CmdCreatePointFamilyInstanceOnFace);
